#include "Flyweight.h"

void UnsharedConcreteFlyweight::operation()
{
    std::cout << "Unshared Flyweight with state " << state_ << std::endl;
}

void ConcreteFlyweight::operation()
{
    std::cout << "Concrete Flyweight with state " << state_ << std::endl;
}

FlyweightFactory::~FlyweightFactory()
{
    for (auto it = flies_.begin(); it != flies_.end(); it++)
    {
        delete it->second;
    }

    flies_.clear();
}

Flyweight *FlyweightFactory::getFlyweight(const int key)
{
    if (flies_.find(key) != flies_.end())
    {
        return flies_[key];
    }

    Flyweight *fly = new ConcreteFlyweight(key);
    flies_.insert(std::pair<int, Flyweight *>(key, fly));

    return fly;
}